const db = require("../db/index");

//查找评论
exports.searchcomment = (req, res) => {
  const sql =
    "select comment.*,user1.username,user1.face,user2.username as toUsername from comment,user as user1,user as user2 where comment.articleid = ? and comment.authorid = user1.id and comment.toUserid = user2.id and comment.status = 1 order by comment.createdate desc";
  db.query(sql, req.params.id, (err, result) => {
    if (err)
      return res.send({
        code: 500,
        message: "数据库异常" + err.message,
      });
    for (let i = 0; i < result.length; i++) {
      result[i] = {
        id: result[i].id,
        content: result[i].content,
        level: result[i].level,
        toUserid: result[i].toUserid,
        toUsername: result[i].toUsername,
        author: {
          id: result[i].authorid,
          face: result[i].face,
          username: result[i].username,
        },
        parentid: result[i].parentid,
        createdate: result[i].createdate,
      };
    }
    res.send({
      code: 200,
      message: "查找评论成功",
      data: result,
    });
  });
};
//添加
exports.addcomment = (req, res) => {
  // //需要前端的数据格式
  // {
  // 	content:'',
  //  articleid:'',
  //  parentid:'',
  //		toUserid:'',
  //		toUsername:'',
  //  level:'',
  // }
  console.log(req.body)
  const sql = "insert into comment set ?";
  const sql2 = "insert into message set ?";
  req.body.authorid = req.user.id;
  db.getConnection(function (err, connection) {
    if (err) {
      return res.send({
        code: 1003,
        message: "数据库异常",
      });
    }
    connection.beginTransaction((err) => {
      //开启事务
      if (err) {
        return res.send({
          code: 1003,
          message: "数据库异常",
        });
      } else {
        connection.query(sql, req.body, (e, result1) => {
          if (e) {
            res.send({ code: 1003, message: "评论失败，请稍后重试" });
            console.log(e);
            return connection.rollback(() => {
              console.log("插入失败数据回滚1");
            });
          } else {
            let tmp = {
              founderid: req.user.id,
              articleid: req.body.articleId,
              toUserid: req.body.toUserid,
              type: 2,
              typeid: result1.insertId,
              intro: req.body.content,
            };
            connection.query(sql2, tmp, (e) => {
              if (e) {
                res.send({ code: 1003, message: "评论失败，请稍后重试" });
                return connection.rollback(() => {
                  console.log("插入失败数据回滚2"+e);
                });
              } else {
                connection.commit((error) => {
                  if (error) {
                    console.log("事务提交失败");
                  }
                });
                connection.release(); // 释放链接

                return res.send({
                  code: 200,
                  message: "评论成功",
                  id: result1.insertId,
                  success: true,
                }); // 返回数据库操作结果这里数据格式可根据个人或团队规范来定制
              }
            });
          }
        });
      }
    });
  });
};
//删除
exports.deletecomment = (req, res) => {
  const sql = "select * from comment where id = ?";
  db.query(sql, req.body.id, (err, result) => {
    if (err)
      return res.send({
        code: 500,
        message: "数据库异常" + err.message,
      });
    if (result[0].authorid != req.user.id)
      return res.send({
        code: 401,
        message: "无权限删除他人评论",
      });
    const sqlstr = "update comment set status = 0 where id = ?";
    db.query(sqlstr, req.body.id, (err, result) => {
      if (err)
        return res.send({
          code: 500,
          message: "数据库异常" + err.message,
        });
      if (result.affectedRows !== 1)
        return res.send({
          code: 1002,
          message: "删除评论失败",
        });
      res.send({
        code: 200,
        message: "删除评论成功",
      });
    });
  });
};
